<!--
 * @description  : 案件-指定管辖
 * @descriptionDetail: 案件-指定管辖
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-08-25 15:43:11
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="administer">
      <u-form label-width="auto" style="padding:0px 15px" :model="adminForm" ref="adminFormRef">
        <u-form-item :required="true" label="文书号" prop="fullDocNo">
          <u-input input-align="right" :disabled="!state.canEdit" type="text" v-model="adminForm.fullDocNo" />
        </u-form-item>
        <u-form-item :required="true" label="通知单位" prop="orgShortName">
          <u-input
            input-align="right"
            type="select"
            placeholder="请选择"
            v-model="adminForm.orgShortName"
            @click="
              () => {
                if (state.canEdit) selectShow('orgShortName');
              }
            "
          />
        </u-form-item>
        <u-form-item :required="true" label="被通知单位" prop="receiveOrg">
          <u-input
            input-align="right"
            :disabled="!state.canEdit"
            type="text"
            v-model="adminForm.receiveOrg"
          />
        </u-form-item>
        <u-form-item :required="true" label="当事人" prop="party">
          <u-input input-align="right" :disabled="!state.canEdit" type="text" v-model="adminForm.party" />
        </u-form-item>
        <u-form-item
          :required="true"
          label="案由"
          label-position="top"
          prop="causeOfAction"
          ref="causeOfActionRef"
        >
          <u-input
            type="textarea"
            placeholder="请选择案由"
            v-model="adminForm.causeOfAction"
            @click="
              () => {
                if (state.canEdit) causeOfActionHandle();
              }
            "
          />
        </u-form-item>
        <u-form-item :required="true" label="原因" label-position="top">
          <u-radio-group :wrap="true" width="100%" v-model="adminForm.disputeType">
            <u-form-item prop="disputeOrg01" :border-bottom="false">
              <u-radio name="1" @change="reasonRadioChange">
                <text>你局与</text>
                <u-input
                  class="input-text"
                  style="width: 150px;"
                  placeholder="请选择"
                  v-model="adminForm.disputeOrg01"
                  @click="
                    () => {
                      if (state.canEdit) selectShow('disputeOrg01');
                    }
                  "
                />
                <text>烟草专卖局均有管辖权并发生管辖权争议</text>
              </u-radio>
            </u-form-item>
            <u-form-item prop="disputeOrg02" :border-bottom="false">
              <u-radio name="2" @change="reasonRadioChange">
                你局与
                <u-input
                  class="input-text"
                  style="width: 150px;"
                  placeholder="请选择"
                  v-model="adminForm.disputeOrg02"
                  @click="
                    () => {
                      if (state.canEdit) selectShow('disputeOrg02');
                    }
                  "
                />
                烟草专卖局发生管辖权争议且未能就该案管辖权争议协商一致
              </u-radio>
            </u-form-item>
            <u-form-item prop="disputeOrg03" :border-bottom="false">
              <u-radio name="3" @change="reasonRadioChange">
                <u-input
                  class="input-text"
                  placeholder="请选择"
                  style="width: 150px;"
                  v-model="adminForm.disputeOrg03"
                  @click="
                    () => {
                      if (state.canEdit) selectShow('disputeOrg03');
                    }
                  "
                />
                烟草专卖局由于
                <u-input
                  class="input-text"
                  type="text"
                  style="width: 150px;"
                  :disabled="!state.canEdit"
                  v-model="adminForm.disJurisdictionReason"
                />
                原因不能或者不宜管辖
              </u-radio>
            </u-form-item>
          </u-radio-group>
        </u-form-item>
        <u-form-item label-position="top" :required="true" label="依据法律" prop="appointBase">
          <u-radio-group width="100%" v-model="adminForm.appointBase">
            <u-radio name="01" @change="legalRadioChange">
              《中华人民共和国行政处罚法》第二十五条第二款之规定
            </u-radio>
            <u-radio name="02" @change="legalRadioChange">
              《烟草专卖行政处罚程序规定》第十一条之规定
            </u-radio>
          </u-radio-group>
        </u-form-item>
        <u-form-item :required="true" label="文书日历" right-icon="arrow-right" prop="docDate">
          <u-calendar mode="date" max-date="2099-12-31" v-model="isShowTime" @change="timeChange">
          </u-calendar>
          <u-input
            :disabled="!state.canEdit"
            placeholder="请选择文书日历"
            inputAlign="right"
            v-model="adminForm.docDate"
            @click="
              () => {
                isShowTime = true;
              }
            "
          />
        </u-form-item>
      </u-form>
    </view>
    <view class="bottom-btn-info">
      <u-row gutter="16">
        <u-col span="6">
          <u-button plain type="primary" @click="submitBtn('save')">
            保存
          </u-button>
        </u-col>
        <u-col span="6">
          <u-button type="primary" @click="submitBtn('submit')">
            提交
          </u-button>
        </u-col>
      </u-row>
    </view>
    <!-- 通知单位选择 -->
    <u-popup mode="bottom" height="80%" v-model="orgSelectShow">
      <org-select :isHybrid="true" limitNodeType="all" treeType="zm-case" @choose="handlerChooseOrg"></org-select>
    </u-popup>
  </view>
</template>

<script>
import OrgSelect from '@/components/org-select.vue';
import caseService from '@/service/case/case.service';
import caseCommon from '../caseCommonMixin.js';
export default {
  components: {
    OrgSelect,
  },
  mixins: [caseCommon,],
  data() {
    return {
      caseParam: {}, // 上个页面传过来的案件信息
      isShowTime: false,
      orgSelectShow: false,
      selectShowType: '',
      isAction: false,
      adminForm: {
        fullDocNo: '',
        orgShortName: '',
        orgShortUuid: '',
        receiveOrg: '',
        party: '',
        disputeType: '1',
        causeOfAction: '',
        disJurisdictionReason: '',
        appointBase: '01',
        docDate: '',
        disputeOrg01: '',
        disputeOrg02: '',
        disputeOrg03: '',
      },
      rules: {
        fullDocNo: [{
          required: true,
          message: '请选择文书号',
          trigger: 'blue',
        },],
        orgShortName: [{
          required: true,
          message: '请选择通知单位',
          trigger: ['blue', 'change',],
        },],
        receiveOrg: [{
          required: true,
          message: '请填写被通知单位',
          trigger: 'blue',
        },],
        party: [{
          required: true,
          message: '请填写当事人',
          trigger: 'blue',
        },],
        causeOfAction: [{
          required: true,
          message: '请选择案由',
          trigger: 'blue',
        },],
        docDate: [{
          required: true,
          message: '请选择案由',
          trigger: ['change',],
        },],
        disputeOrg01: [{
          validator: (rule, value, callback) => {
            return this.adminForm.disputeType == '1' && this.$u.test.empty(value) ? false :
              true;
          },
          message: '原因选择不能为空',
          trigger: ['change', 'blur',],
        },],
        disputeOrg02: [{
          validator: (rule, value, callback) => {
            return this.adminForm.disputeType == '2' && this.$u.test.empty(value) ? false :
              true;
          },
          message: '原因选择不能为空',
          trigger: ['change', 'blur',],
        },],
        disputeOrg03: [{
          validator: (rule, value, callback) => {
            return this.adminForm.disputeType == '3' && this.$u.test.empty(value) ? false :
              true;
          },
          message: '原因选择不能为空',
          trigger: ['change', 'blur',],
        },],
      },
    };
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.adminFormRef.setRules(this.rules);
  },
  onLoad() {
    this.caseParam = this.getRouteParam();
  },
  watch: {
    isAction() {
      this.$refs.causeOfActionRef.validation();
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      // 准备数据
      if (this.caseParam.bizUuid) {
        caseService.getSourceDocIns(this.caseParam.bizUuid).then((res) => {
          if (res.success) {
            this.caseParam.id = res.bean;
            this.initDocInfo();
            this.getReceiveOrg();
          }
        });
      } else {
        this.initDocInfo();
        this.getReceiveOrg();
      }
    },
    getReceiveOrg() {
      caseService.getReceiveOrg().then((res) => {
        this.adminForm.receiveOrg = res.data[0].receiveOrg;
        this.adminForm.docDate = res.data[0].docDate;
      });
    },
    initDocInfo() {
      this.docStep((_doc) => {
        caseService.getAssignarea(this.caseParam).then((res) => {
          // 有2个元素 说明是添加类型的
          if (res.success && res.data.length == 2) {
            // 取第二个
            let rs = res.data[1];
            for (let k in rs) {
              if (Object.prototype.hasOwnProperty.call(this.adminForm, k)) {
                this.adminForm[k] = rs[k];
              } else {
                this.$set(this.adminForm, k, rs[k]);
              }
            }
          } else {
            let rs = res.data[0];
            // 说明已经有数据了
            for (let k in rs) {
              if (Object.prototype.hasOwnProperty.call(this.adminForm, k)) {
                this.adminForm[k] = rs[k];
              } else {
                this.$set(this.adminForm, k, rs[k]);
              }
            }
          }
          this.$modal.hideLoading();
        });
      });
    },
    // 案件性质点击事件
    causeOfActionHandle() {
      this.isAction = true;
      this.$u.route('pages/case/components/casePropertySelection');
    },
    // 选择案件性质返回的数据
    updateBackState(param) {
      if (param.caseSelectionData) {
        let [names, ids,] = [
          [],
          [],
        ];
        param.caseSelectionData.forEach((item) => {
          names.push(item.caseProperty);
          ids.push(item.casepropUuid);
        });
        this.adminForm.appointPropertyRelList = param.caseSelectionData;
        this.$set(this.adminForm, 'causeOfAction', names.join(','));
        this.$set(this.adminForm, 'caseProp', ids.join(','));
        this.isAction = false;
      }
    },
    // 选择时间
    timeChange(e) {
      this.adminForm.docDate = e.result.replace('-', '年').replace('-', '月') + '日';
    },
    // 弹窗类型选择
    selectShow(type) {
      this.orgSelectShow = true;
      this.selectShowType = type;
    },

    // 选择通知单位
    handlerChooseOrg(orgs) {
      if (this.selectShowType == 'orgShortName') {
        this.adminForm.orgShortUuid = orgs[0].id;
        this.adminForm.orgShortName = orgs[0].name;
      } else if (this.selectShowType == 'disputeOrg01') {
        this.adminForm.disputeOrg01 = orgs[0].name;
      } else if (this.selectShowType == 'disputeOrg02') {
        this.adminForm.disputeOrg02 = orgs[0].name;
      } else {
        this.adminForm.disputeOrg03 = orgs[0].name;
      }
      this.orgSelectShow = false;
    },
    // 选中某个原因单选框时，由radio时触发
    reasonRadioChange(e) {
      this.adminForm.disputeType = e;
      if (e == '3') {
        this.adminForm.appointBase = '02';
      } else {
        this.adminForm.appointBase = '01';
      }
    },
    // 选中某个依法条例单选框是，由radio时触发
    legalRadioChange(e) {
      console.log(e);
    },
    submitBtn(isSubmit) {
      this.$refs.adminFormRef.validate((valid) => {
        if (!valid) return;
        this.$modal.showLoading('保存中...');
        // debugger
        caseService
          .saveAssignarea(this.adminForm)
          .then((dr) => {
            this.$modal.hideLoading();
            if (!dr.success) {
              this.$modal.alert('保存失败:' + dr.msg);
              return;
            }
            this.adminForm.transUuid = dr.bean.transUuid;
            this.adminForm.cluesUuid = dr.bean.cluesUuid;
            if (isSubmit == 'submit') {
              this.$modal.showLoading('提交中...');
              caseService
                .submitAssignarea(this.adminForm, dr.data[0].docUuid, dr.data[0]
                  .docInsUuid)
                .then((sdr) => {
                  this.$modal.hideLoading();
                  this.$modal.alert('提交成功', () => {
                    this.backPrev({
                      paramKey: 'refreshBack',
                      param: true,
                    });
                  });
                })
                .catch((error) => {
                  this.$modal.hideLoading();
                  this.$modal.alert('保存失败:' + error);
                  console.error(error);
                });
            } else {
              this.$modal.alert('保存成功', () => {
                this.backPrev({
                  paramKey: 'refreshBack',
                  param: true,
                });
              });
            }
          })
          .catch((err) => {
            this.$modal.hideLoading();
            this.$modal.alert('保存失败:' + err);
            console.error(err);
          });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
	.input-text {
		border-bottom: 1px solid black;
		text-align: center !important;
		display: inline-flex;
		width: auto;
		flex: none;

		/deep/ .uni-input-input {
			border-bottom: 1px solid #999;
		}
	}
</style>
